package com.clkj.cldisabledmanclient.mvp.satisfactionresearch;

import android.app.ProgressDialog;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.clkj.cldisabledmanclient.R;
import com.clkj.cldisabledmanclient.app.CustomApplication;
import com.clkj.cldisabledmanclient.base.BaseActivity;
import com.clkj.cldisabledmanclient.constant.Config;
import com.clkj.cldisabledmanclient.constant.ShareKey;
import com.clkj.cldisabledmanclient.http.HttpConfig;
import com.clkj.cldisabledmanclient.util.CommonUtil;
import com.clkj.cldisabledmanclient.util.ToastUtil;
import com.clkj.cldisabledmanclient.util.scheduler.SchedulerProvider;

import java.util.HashMap;
import java.util.Set;

public class ResearchDetailActivity extends BaseActivity implements ResearchDetailContract.View {

    private String qid;

    private ResearchDetailContract.Presenter mPresenter;
    private WebView wvUrl;

    private ProgressDialog progressDialog;
    private String realName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_research_detail);
        initTitle(null, null, "满意度调查", true, null);
        initData();
        initView();
        assignView();

        getResearchDetail();
    }

    @Override
    public void initData() {

        qid = getIntent().getStringExtra("qid");
        realName = (String) CommonUtil.getShareValue(ShareKey.USER_IDCARD, "");
        mPresenter = new ResearchDetailPresenter(this, SchedulerProvider.getInstance(), CustomApplication.getInstance().getHttpService());
    }

    @Override
    public void assignView() {

    }

    @Override
    public void initView() {

        wvUrl = (WebView) findViewById(R.id.wv_url);
        //CommonUtil.initWebView(wvUrl);

        WebSettings webSettings = wvUrl.getSettings();

        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE); //设置缓存

        webSettings.setJavaScriptEnabled(true);//设置能够解析Javascript

        webSettings.setDomStorageEnabled(true);//设置适应Html5 //重点是这个设置

        /*wvUrl.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });*/

        // 复写WebViewClient类的shouldOverrideUrlLoading方法
        wvUrl.setWebViewClient(new WebViewClient() {
                                   @Override
                                   public boolean shouldOverrideUrlLoading(WebView view, String url) {

                                       // 步骤2：根据协议的参数，判断是否是所需要的url
                                       // 一般根据scheme（协议格式） & authority（协议名）判断（前两个参数）
                                       //假定传入进来的 url = "js://webview?arg1=111&arg2=222"（同时也是约定好的需要拦截的）
                                       Log.e("WebViewClient", url);

                                       if ("js://webview?opstion=close".equals(url.trim())) {
                                           setResult(RESULT_OK);
                                           finish();
                                       }
                                      /* Uri uri = Uri.parse(url);
                                       // 如果url的协议 = 预先约定的 js 协议
                                       // 就解析往下解析参数
                                       if ("js".equals(uri.getScheme())) {

                                           // 如果 authority  = 预先约定协议里的 webview，即代表都符合约定的协议
                                           // 所以拦截url,下面JS开始调用Android需要的方法
                                           if ("webview".equals(uri.getAuthority())) {

                                               //  步骤3：
                                               // 执行JS所需要调用的逻辑
                                               System.out.println("js调用了Android的方法");
                                               // 可以在协议上带有参数并传递到Android上
                                               HashMap<String, String> params = new HashMap<>();
                                               Set<String> collection = uri.getQueryParameterNames();

                                               for (String str : collection) {
                                                   Log.e("fanhui", str);
                                               }
                                           }

                                           return true;
                                       }*/
                                       return super.shouldOverrideUrlLoading(view, url);
                                   }
                               }

        );

    }

    @Override
    public void getResearchDetail() {

        mPresenter.getResearchDetail(qid, realName);
    }

    @Override
    public void getResearchDetailSuccess(String url) {

        wvUrl.loadUrl(url);
    }

    @Override
    public void getResearchDetailError(String tip) {

        ToastUtil.showShort(this, tip);
    }

    @Override
    public void setPresenter(ResearchDetailContract.Presenter presenter) {

        this.mPresenter = presenter;
    }

    @Override
    public void showLoading() {
        progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setIndeterminate(false);
        progressDialog.setCancelable(true);
        //设置点击进度对话框外的区域对话框不消失
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.setMessage("正在加载...");
        progressDialog.show();
    }

    @Override
    public void dismissLoading() {
        if (progressDialog != null) {
            progressDialog.dismiss();
            progressDialog = null;
        }
    }

    public void finishPage() {
        finish();
    }
}
